﻿using UnityEngine;
using UnityEditor;
using UnityEngine.AI;

namespace GameToolkit.Editor
{
    [CustomPropertyDrawer(typeof(NavAgentTypeAttribute))]
    public class NavAgentTypeDrawer : PropertyDrawer
    {
        public static string[] agentNames;
        public static int[] agentIds;

        public static void GetAgentNames()
        {
            int len = agentNames == null ? 0 : agentNames.Length;
            var count = NavMesh.GetSettingsCount();
            if (count != len || agentNames == null || agentIds == null)
            {
                agentNames = new string[count];
                agentIds = new int[count];
            }
        }

        public static void GetAgentNames(int agentId, out int agentIndex)
        {
            agentIndex = -1;
            GetAgentNames();
            for (int i = 0; i < agentNames.Length; i++)
            {
                agentIds[i] = NavMesh.GetSettingsByIndex(i).agentTypeID;
                agentNames[i] = NavMesh.GetSettingsNameFromID(agentIds[i]);
                if (agentId == agentIds[i])
                    agentIndex = i;
            }
        }

       public static int GetAgentMask(SerializedProperty listInteger)
        {
            int mask = 0;
            var agents = NavAgentTypeDrawer.agentIds;
            for (int i = 0; i < agents.Length; i++)
            {
                for (int j = 0; j < listInteger.arraySize; j++)
                {
                    if (listInteger.GetArrayElementAtIndex(j).intValue == agents[i])
                    {
                        mask |= (1 << i);
                        break;
                    }
                }
            }
            return mask;
        }

        public static void SetAgentMask(SerializedProperty listInteger, int mask)
        {
            listInteger.arraySize = 0;
            for (int i = 0; i < agentIds.Length; i++)
            {
                if ((mask & (1 << i)) == 0)
                    continue;
                listInteger.arraySize++;
                listInteger.GetArrayElementAtIndex(listInteger.arraySize - 1).intValue = agentIds[i];
            }
        }

        int agentIndex;
        
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            EditorGUI.BeginProperty(position, label, property);
            int lv = EditorGUI.indentLevel;
            position = EditorGUI.PrefixLabel(position, label);
            EditorGUI.indentLevel = 0;
            position.width -= 80;
            if (property.propertyType == SerializedPropertyType.Integer)
            {
                GetAgentNames(property.intValue, out agentIndex);
                int index = EditorGUI.Popup(position, agentIndex, agentNames);
                if (index != agentIndex && index >= 0)
                    property.intValue = agentIds[index];
                position.x += position.width;
                position.width = 80;
                EditorGUI.LabelField(position, agentIds[index].ToString(), DevilEditorUtility.HintStyle("label"));
            }
            else
            {
                EditorGUI.LabelField(position, "Use Integer Instead of " + property.propertyType);
            }
            EditorGUI.indentLevel = lv;

            EditorGUI.EndProperty();
        }
    }
}